package com.cqhilink.iot.jobstandardmanagement.services.innerservice.permission.util;


import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.cqhilink.iot.jobstandardmanagement.common.bean.ApplicationContextHelper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.SystemPlatform;
import com.cqhilink.iot.jobstandardmanagement.facade.webservicesmodel.UasApp;
import com.cqhilink.iot.jobstandardmanagement.facade.service.permission.JsmSystemPlatformService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/2/17.
 */
public class SystemPlatformPool implements Runnable {
    private List<UasApp> list;
    private String controlType;//1.添加，2.修改，3.删除

    public void setList(List<UasApp> list) {
        this.list = list;
    }

    public void setControlType(String controlType) {
        this.controlType = controlType;
    }

    @Override
    public void run() {
        JsmSystemPlatformService systemPlatformService = (JsmSystemPlatformService) ApplicationContextHelper.getBean("systemPlatformService");
        if (systemPlatformService != null && StringUtils.isNotEmpty(controlType)) {
            List<SystemPlatform> paramList = new ArrayList<SystemPlatform>();
            try {
                systemPlatform(list, paramList);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (paramList.size() > 0) {
                if ("1".equals(controlType)) {
                    //添加
                    systemPlatformService.insertList(paramList);
                } else if ("2".equals(controlType)) {
                    //修改
                    systemPlatformService.updateList(paramList);
                } else if ("3".equals(controlType)) {
                    //删除
                    systemPlatformService.deleteList(paramList);
                }
            }
        }
    }

    private void systemPlatform(List<UasApp> list, List<SystemPlatform> paramList) throws Exception {
        ConvertUtils.register(new DateConverter(null), Date.class);
        for (UasApp ua : list) {
            SystemPlatform param = new SystemPlatform();
            BeanUtils.copyProperties(param, ua);
            param.setSystemPlatformId(ua.getAppId());
            param.setSystemPlatformName(ua.getAppName());
            param.setSystemCode(ua.getAppCode());
            param.setSystemPlatformAddr(ua.getMainPage());
            param.setRemark(ua.getMemo());
            param.setCreatetime(DateUtil.StringToDate(ua.getCreateDate(), DateStyle.YYYY_MM_DD_HH_MM_SS));
            param.setCreator(ua.getCreator());
            param.setModifyer(ua.getModifier());
            param.setModifytime(DateUtil.StringToDate(ua.getModifyDate(), DateStyle.YYYY_MM_DD_HH_MM_SS));
            param.setWebserviceCode(ua.getServicesno());
//            param.setRoleId(ua.getAdminrole());
            param.setParentPlatformId(ua.getParentCode());
            paramList.add(param);
        }
    }
}
